package com.interfaces;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.web.CommonJspKeys;
import com.common.web.CommonKeys;
import com.common.web.Json;
import com.common.web.ReturnCodes;
import com.core.role.bo.RoleBo;
import com.core.user.UserJspKeys;
import com.core.user.bo.UserBo;
import com.core.user.dto.UserDto;
import com.core.user.dto.UserSearchCondition;
import com.core.user.pojo.User;
import com.util.GsonUtil;
import com.util.exception.ComRuntimeException;
import com.util.exception.ComSystemException;
import com.util.framework.BaseController;

@Controller
public class UserController extends BaseController{
	protected Logger log = Logger.getLogger(this.getClass());
    @Resource
    private UserBo userBo;
    @Resource
    private RoleBo roleBo;
    public static final String URL_ENTITY = "/user";
    public static final String URL_GOTO_MAIN_PAGE = URL_ENTITY + "/goto_main_page";
    public static final String URL_GOTO_ADD_UPDATE_INDEX_PAGE = URL_ENTITY + "/goto_add_update_index_page";
    public static final String URL_GOTO_ADD_UPDATE_PAGE = URL_ENTITY + "/goto_add_update_page";
    public static final String URL_GOTO_ADD_UPDATE_ROLE_PAGE = URL_ENTITY + "/goto_add_update_role_page";
    public static final String URL_GOTO_ADD_PAGE = URL_ENTITY + "/goto_add_page";
    public static final String URL_GOTO_UPDATE_PAGE = URL_ENTITY + "/goto_update_page";
    public static final String URL_GOTO_UPDATE_PASSWORD_PAGE = URL_ENTITY + "/goto_update_password_page";

    public static final String URL_ADD = URL_ENTITY + "/add";
    public static final String URL_UPDATE = URL_ENTITY + "/update";
    public static final String URL_ADD_UPDATE_ROLE_BAT = URL_ENTITY + "/add_update_role_bat";
    public static final String URL_UPDATE_PASSWORD = URL_ENTITY + "/update_password";
    public static final String URL_DELETE = URL_ENTITY + "/delete";
    public static final String URL_MAIN_SEARCH = URL_ENTITY + "/main_search";
	public static final String URL_NAME = URL_ENTITY +"/getName";
	public static final String URL_USER = URL_ENTITY +"/getUser";
    @RequestMapping(value = URL_GOTO_MAIN_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoMainPage(HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        ModelAndView modelAndView = new ModelAndView("user/user_main");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        return modelAndView;
    }
    @RequestMapping(value = URL_GOTO_ADD_UPDATE_INDEX_PAGE, method = RequestMethod.GET)
    public ModelAndView URL_GOTO_ADD_UPDATE_INDEX_PAGE(User user,HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        ModelAndView modelAndView = new ModelAndView("user/user_add_update_index");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        modelAndView.addObject(UserJspKeys.JspParam_UserInfo, user);
        return modelAndView;
    }
    
    @RequestMapping(value = URL_GOTO_ADD_UPDATE_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoAddUpdatePage(User user, HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        String url="";
        User user1 = userBo.getById(user);
        if(user1==null){
        	user1=user;
        	url="user/user_add";
        }else{
        	url="user/user_update";
        }
        ModelAndView modelAndView = new ModelAndView(url);
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        modelAndView.addObject(UserJspKeys.JspParam_UserInfo, user1);
        return modelAndView;
    }
    
    @RequestMapping(value = URL_GOTO_ADD_UPDATE_ROLE_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoAddUpdateRolePage(User user, HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        ModelAndView modelAndView = new ModelAndView("user/user_add_update_role");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        modelAndView.addObject(UserJspKeys.JspParam_UserInfo, user);
        return modelAndView;
    }
    

    @RequestMapping(value = URL_GOTO_ADD_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoAddPage(HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);

        ModelAndView modelAndView = new ModelAndView("user/user_add");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        return modelAndView;
    }
    @RequestMapping(value = URL_GOTO_UPDATE_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoUpdatePage(User user, HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);

        User user1 = userBo.getById(user);

        ModelAndView modelAndView = new ModelAndView("user/user_update");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        modelAndView.addObject(UserJspKeys.JspParam_UserInfo, user1);
        return modelAndView;
    }
    @RequestMapping(value = URL_GOTO_UPDATE_PASSWORD_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoUpdatePwdPage(HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);

        ModelAndView modelAndView = new ModelAndView("user/user_update_pwd");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = URL_UPDATE_PASSWORD, method = RequestMethod.POST)
    public Json updatePwd(String oldPwd, String newPwd, HttpSession session) {
        Json json = new Json();
        try {
            User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
            userBo.updatePwd(userInfo.getLoginName(), oldPwd, newPwd);
            json.setSuccess(true);
        } catch (ComRuntimeException e) {
            json = new Json(e.getErrorCode(), e.getMessage());
        } catch (ComSystemException e) {
            log.error("error", e);
            json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
        } catch (Throwable e) {
            log.error("error", e);
            json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
        }
        return json;
    }
    
    @ResponseBody
    @RequestMapping(value = URL_MAIN_SEARCH, method = RequestMethod.POST,produces={"application/json;charset=utf-8"})
    public String getUser(UserSearchCondition usc, HttpSession session) {
        String msg="";
        try {
            List<UserDto> userList = userBo.searchDto(usc);
            Integer totalRecordCount=userBo.searchCnt(usc);
            if(userList.size()==0){
            	 msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
            }else{
            	msg=GsonUtil.gson.toJson(userList);
                msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":"+totalRecordCount+",\"recordsFiltered\":"+totalRecordCount+",\"data\": "+msg+"}";
            }
            
        } catch (Exception e) {
            log.error("error", e);
            msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
        }
        return msg;
    }
    @ResponseBody
    @RequestMapping(value = URL_ADD, method = RequestMethod.POST)
    public Json add(User user, HttpSession session) {
        Json json = new Json();
        try {
            User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
            user.setAddUserId(userInfo.getId());
            user.setAddTime(new Date());
            userBo.add(user);
            User user1=userBo.getByUserName(user.getLoginName());
            json.setObj(user1.getId());
            json.setSuccess(true);
        } catch (ComRuntimeException e) {
            json = new Json(e.getErrorCode(), e.getMessage());
        } catch (ComSystemException e) {
            log.error("error", e);
            json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
        } catch (Throwable e) {
            log.error("error", e);
            json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
        }
        return json;
    }
    @ResponseBody
    @RequestMapping(value = URL_NAME, method = RequestMethod.POST)
    public String getName(HttpSession session){
    	User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
    	return userInfo.getLoginName();
    }
    @ResponseBody
    @RequestMapping(value = URL_USER, method = RequestMethod.POST)
    public String getUser(HttpSession session){
    	String msg="";
    	User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
    	User userInfo2 = userBo.getById(userInfo);
    	userInfo2.setRoleList(roleBo.searchPojoByUserId(userInfo2));
    	msg=GsonUtil.gson.toJson(userInfo2.getRoleList());
    	return msg;
    }
    @ResponseBody
    @RequestMapping(value = URL_UPDATE, method = RequestMethod.POST)
    public Json update(User user, HttpSession session) {
        Json json = new Json();
        try {
        	User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        	user.setAddUserId(userInfo.getId());
            user.setAddTime(new Date());
            userBo.update(user);
            json.setSuccess(true);
        } catch (ComRuntimeException e) {
            json = new Json(e.getErrorCode(), e.getMessage());
        } catch (ComSystemException e) {
            log.error("error", e);
            json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
        } catch (Throwable e) {
            log.error("error", e);
            json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
        }
        return json;
    }

    @ResponseBody
    @RequestMapping(value = URL_DELETE, method = RequestMethod.POST)
    public Json delete(User user) {
        Json json = new Json();
        try {
            userBo.delete(user);
            json.setSuccess(true);
        } catch (ComRuntimeException e) {
            json = new Json(e.getErrorCode(), e.getMessage());
        } catch (ComSystemException e) {
            log.error("error", e);
            json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
        } catch (Throwable e) {
            log.error("error", e);
            json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
        }
        return json;
    }
}
